var express = require('express');
var router = express.Router();
var query=require('../db/pool').query
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.post('/login', function (req, res, next) {
  // console.log(req.body)//请求参数=>req.body
  var sql = "select * from user where username=? and password=?"
  var val = [req.body.username, req.body.password]
  query(sql, val, function (err, data) {
    if (err) throw err;
    // console.log(data);
    // console.log(data.length);
    if (data.length === 0) {
      res.json({
        code: 99,
        msg: "账号密码错误",
        data:[]
      })
    } else {
      req.session.user_id=data[0].user_id
      // console.log(data)
      res.json({
        code: 66,
        msg: "登入成功",
        data:[data]
      })
    }
  })
})
module.exports = router;
